package services;

import dao.*;
import models.*;

import play.db.jpa.JPAApi;
import vo.ContractInfo_C_Entity;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.Query;

import java.util.List;
import java.util.concurrent.CompletionStage;
import java.util.function.Function;
import java.util.stream.Stream;

import static java.util.concurrent.CompletableFuture.supplyAsync;


/**
 * Provide JPA operations running inside of a thread pool sized to the connection pool
 */
public class ContractInfo_Repository_JPA implements ContractInfo_Repository {

    private final JPAApi jpaApi;
    private final DatabaseExecutionContext executionContext;

    @Inject
    public ContractInfo_Repository_JPA(JPAApi jpaApi, DatabaseExecutionContext executionContext) {
        this.jpaApi = jpaApi;
        this.executionContext = executionContext;
    }


    
    @Override
    public CompletionStage<List<ContractInfo_C_Entity>> getContractList ()
    {
    	return supplyAsync(() -> {
            return jpaApi.withTransaction(entityManager -> {
            	//return entityManager.createQuery("select c from ContractInfo_T_Entity c ", ContractInfo_T_Entity.class).getResultList();
            	return entityManager.createQuery("select new vo.ContractInfo_C_Entity(c.fdRowId, p.fdProductName, c.deleted, c.fdContractNumber, c.fdDeptAid, c.fdDeptBid, c.fdDestination, c.fdExcutedQty, c.fdLoadPlace, c.fdModifiedDate, c.fdProductId, c.fdQuantity, c.fd_LastDay, d.fdDeptName) from ContractInfo_T_Entity c ,ProductInfo_T_Entity p, DeptInfo_T_Entity d where c.fdProductId = p.fdRowId and c.fdDeptBid = d.fdDeptId", ContractInfo_C_Entity.class).getResultList();           	
            });
        }, executionContext);    	
    }
}